package com.example.sport.controller;

import com.example.sport.domain.AthleteEventResult;
import com.example.sport.domain.College;
import com.example.sport.domain.Result;
import com.example.sport.domain.User;
import com.example.sport.service.AthleteService;
import com.example.sport.service.CollegeService;
import com.example.sport.service.UserService;
import com.example.sport.utils.JwtUtil;
import com.example.sport.utils.MD5Utils;
import com.example.sport.utils.ThreadLocalUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.constraints.Pattern;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.URL;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;


@Tag(name = "运动员相关", description = "这是描述")
@RestController
@RequestMapping("/athlete")
@Validated
@Slf4j
//请求值校验
public class AthleteController {

    @Autowired
    private UserService userService;

    @Operation(summary = "用户信息查询")
    @GetMapping("/my_info")
    public Result<User> userInfo() {
        //查询用户
        Map<String, Object> map = ThreadLocalUtil.get();
        String username = (String) map.get("username");
        User tuser = userService.findUserByName(username);
        return Result.success(tuser);
    }

    @Autowired
    private CollegeService collegeService;
    @Operation(summary = "学院信息查询")
    @GetMapping("/my_college_info")
    public Result<College> myCollageInfo() {
        //查询用户
        Map<String, Object> map = ThreadLocalUtil.get();
        Integer userId = (Integer) map.get("id");
        College college = collegeService.getCollegeInfoByUserId(String.valueOf(userId));
        return Result.success(college);
    }

    @Autowired
    private AthleteService athleteService;
    @Operation(summary = "结果查询")
    @GetMapping("/my_college_result")
    public Result<List<AthleteEventResult>> myCollegeEvent() {
        //查询用户
        Map<String, Object> map = ThreadLocalUtil.get();
        Integer userId = (Integer) map.get("id");
        User tuser = userService.findUserById(userId);
        List<AthleteEventResult> list = athleteService.getAthleteEventResultsByCollegeId(String.valueOf(tuser.getCollegeId()));
        return Result.success(list);
    }
    @Operation(summary = "我的赛事结果查询")
    @GetMapping("/my_result")
    public Result<List<AthleteEventResult>> myEvent() {
        //查询用户
        Map<String, Object> map = ThreadLocalUtil.get();
        Integer userId = (Integer) map.get("id");
        return Result.success(athleteService.getAthleteEventResultsByUserId(String.valueOf(userId)));
    }


    //    @ApiOperation(value = "用户信息更新" )
    @Operation(summary = "用户信息更新")
    @PutMapping("/update")
    public Result update(@RequestBody  User user) {
        userService.update(user);
        return Result.success();
    }


    @Operation(summary = "修改密码")
    @PatchMapping("/updatePwd")
    public Result updatePwd(@RequestBody Map<String, String> params) {
        //校验参数
        String oldPwd = params.get("old_pwd");
        String newPwd = params.get("new_pwd");
        String rePwd = params.get("re_pwd");
        if (!StringUtils.hasLength(oldPwd) || !StringUtils.hasLength(newPwd) || !StringUtils.hasLength(rePwd)) {
            return Result.error("缺少必要参数");
        }
        //校验原密码
        Map<String, Object> map = ThreadLocalUtil.get();
        User tuser = userService.findUserByName((String) map.get("username"));
        if (!tuser.getPassword().equals(MD5Utils.getMD5String(oldPwd))) {
            return Result.error("原密码错误");
        }

        if (!newPwd.equals(rePwd)) {
            return Result.error("两次填写的新密码不一样");
        }
        //更新密码

        userService.updatePwd(rePwd);
        return Result.success();
    }


}
